package com.meida.module.bus.provider.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.mybatis.interceptor.GetInterceptor;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.bus.client.entity.BusTerm;
import com.meida.module.bus.client.entity.BusTestpaper;
import com.meida.module.file.client.entity.SysFile;
import com.meida.module.file.provider.service.SysFileService;
import com.meida.module.user.client.entity.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 试卷详情
 *
 * @author zyf
 */
@Component
public class BusTestPaperDetailHandler implements GetInterceptor {

    @Autowired
    private SysFileService sysFileService;


    @Override
    public void prepare(CriteriaQuery cq, EntityMap entityMap) {
        cq.clear();
        cq.select(BusTestpaper.class, "testpaperId", "beginYear", "endYear", "courseName", "professorName", "state", "createTime");
        cq.select(BusTerm.class, "termName");
        cq.select(AppUser.class, "userName");
        cq.like(BusTestpaper.class, "professorName");
        cq.like(BusTestpaper.class, "courseName");
        cq.like(AppUser.class, "userName");
        cq.createJoin(AppUser.class).setMainField("userId").setJoinField("userId");
        cq.createJoin(BusTerm.class).setMainField("termId").setJoinField("termId");
    }

    @Override
    public void complete(CriteriaQuery cq, EntityMap map) {
        Long testpaperId = map.getLong("testpaperId");
        List<EntityMap> fileList = sysFileService.selectFileList(testpaperId, BusTestpaper.class.getSimpleName(), criteriaQuery -> {
            criteriaQuery.select(SysFile.class, "fileId", "fileName", "fileUrl","fileGroup", "ossPath", "createTime");
        });
        map.put("fileList", fileList);
    }
}
